package zuoye;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Modifyjpanel extends JPanel implements ActionListener {
    private Icon sureimage = Util.getIcon("/images/buttonSure.jpg");
    private Icon offimage = Util.getIcon("/images/buttonOff.jpg");
    private JButton SurejButton;
    private JButton OffjButton;
    private Mainjframe frame;
    private JTextField textField1;
    private JTextField textField2;
    private JLabel jLabel;
    private JLabel jLabel1;
    private JTextField textField0;
    private JLabel jLabel0;

    public Modifyjpanel(Mainjframe frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        jLabel0 = new JLabel("请输入旧密码：");
        jLabel0.setForeground(Color.BLACK);
        jLabel0.setFont(new Font("宋体", Font.BOLD, 22));
        jLabel0.setBounds(340, 160, 200, 40);
        this.add(jLabel0);

        textField0 = new JTextField();
        textField0.setBounds(550, 160, 350, 40);
        this.add(textField0);

        jLabel = new JLabel("请输入新密码：");
        jLabel.setForeground(Color.BLACK);
        jLabel.setFont(new Font("宋体", Font.BOLD, 22));
        jLabel.setBounds(340, 220, 200, 40);
        this.add(jLabel);

        textField1 = new JTextField();
        textField1.setBounds(550, 220, 350, 40);
        this.add(textField1);

        jLabel1 = new JLabel("请再次输入新密码：");
        jLabel1.setForeground(Color.BLACK);
        jLabel1.setFont(new Font("宋体", Font.BOLD, 22));
        jLabel1.setBounds(340, 280, 200, 40);
        this.add(jLabel1);

        textField2 = new JTextField();
        textField2.setBounds(550, 280, 350, 40);
        this.add(textField2);

        SurejButton = new JButton(sureimage);
        SurejButton.setBounds(360, 500, 140, 50);
        this.add(SurejButton);
        SurejButton.addActionListener(this);
        OffjButton = new JButton(offimage);
        OffjButton.setBounds(700, 500, 140, 50);
        this.add(OffjButton);
        OffjButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == SurejButton) {
            ArrayList<User> user = (ArrayList<User>) FileUtil.readObject("User.dat");
            if (textField0.getText().equals(user.get(frame.getIndex()).getUserpsw())) {
                if (textField1.getText().equals(textField2.getText())) {
                    if (textField1.getText().matches("^[a-zA-Z]\\w{5,17}$")) {
                        user.get(frame.getIndex()).setUserpsw(textField1.getText());
                        FileUtil.writeObject("User.dat", user);
                        JOptionPane.showMessageDialog(this, "修改密码成功，请重新登录");
                        textField0.setText("");
                        textField1.setText("");
                        textField2.setText("");
                        frame.switchPanel("login");
                    } else {
                        JOptionPane.showMessageDialog(this, "密码格式不对");
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "两次密码不匹配");
                    textField2.setText("");
                }
            } else {
                JOptionPane.showMessageDialog(this, "与原密码不匹配");
            }

        } else if (e.getSource() == OffjButton) {
            frame.switchPanel("menu");
        }
    }
}
